# Base-image
FROM registry.suse.com/bci/ruby:2.5@sha256:a1b8ef3e6682532d655dac67bcebb44e80d6d6d1018433d9b2df9bcdb9feb674 AS base

ARG ATOM_RUBY_VERSION=3.4.5
ARG NODEJS_DIST_URL
ARG NPM_REPO
ARG PIP_CONFIG
ARG RUBY_LANG_URL
ARG RUBYGEMS_REPO
ARG SUSE_REPO

ENV ATOM_RUBY_HOME=/root/.rbenv/versions/$ATOM_RUBY_VERSION \
    ATOM_RUBY_VERSION=$ATOM_RUBY_VERSION \
    BUNDLE_SILENCE_ROOT_WARNING=true \
    JAVA_OPTIONS="-Dhttps.protocols=TLSv1.1,TLSv1.2" \
    LANG=en_US.UTF-8 \
    LANGUAGE=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8 \
    PYTHONPATH=/opt/pypi \
    RUBY_BUILD_BUILD_PATH="/tmp/rbenv" \
    RUBY_BUILD_HTTP_CLIENT=curl \
    npm_config_python=/usr/bin/python3.11
ENV PATH=${PATH}:/usr/local/bin:/root/.local/bin:${PYTHONPATH}/bin:/root/.rbenv/bin

RUN set -e; \
    if [ -n "$NODEJS_DIST_URL" ]; then \
      export "NVM_NODEJS_ORG_MIRROR=$NODEJS_DIST_URL"; \
      printf "disturl=$NODEJS_DIST_URL\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$NPM_REPO" ]; then \
      export "COREPACK_NPM_REGISTRY=$NPM_REPO"; \
      printf "registry=$NPM_REPO\n@jsr:registry=$NPM_REPO\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$PIP_CONFIG" ]; then \
      mkdir $HOME/.pip/; \
      printf "$PIP_CONFIG" > $HOME/.pip/pip.conf; \
    fi; \
    if [ -n "$RUBYGEMS_REPO" ]; then \
      gem sources --add $RUBYGEMS_REPO/; \
      gem sources --remove https://rubygems.org/; \
      gem sources -c; \
    fi; \
    if [ -n "$SUSE_REPO" ]; then \
      sed -i "s|https://public-dl.suse.com|$SUSE_REPO|g" /etc/zypp/repos.d/*; \
      zypper --no-gpg-checks refresh; \
    fi; \
    zypper refresh \
 && zypper \
      --non-interactive \
        update \
 && zypper \
      --non-interactive \
        install \
        -l \
        --no-recommends \
          gawk \
          git-core \
          java-21-openjdk-devel \
          jemalloc-devel \
          libffi-devel \
          libopenssl-devel \
          libyaml-devel \
          make \
          ncurses \
          ncurses-devel \
          nodejs22 \
          npm22 \
          python311 \
          python311-pip \
          readline-devel \
          rust1.85 \
          unzip \
          wget \
          zip \
          zlib-devel \
 && pip install \
      --no-cache-dir \
      --target ${PYTHONPATH} \
      --upgrade \
        atom-tools \
 && npm install -g \
      corepack \
 && gem install bundler -v 2.3.27 \
 && gem install rubygems-update -v 3.3.27 \
 && bundle config git.allow_insecure true \
 && git clone https://github.com/rbenv/rbenv.git --depth=1 ~/.rbenv \
 && echo 'export PATH="/root/.rbenv/bin:$PATH"' >> ~/.bashrc \
 && echo 'eval "$(/root/.rbenv/bin/rbenv init - bash)"' >> ~/.bashrc \
 && source ~/.bashrc \
 && mkdir -p "$(rbenv root)/plugins" \
 && git clone https://github.com/rbenv/ruby-build.git --depth=1 "$(rbenv root)/plugins/ruby-build" \
 && if [ -n "$RUBY_LANG_URL" ]; then \
      sed -i "s|https://cache.ruby-lang.org|$RUBY_LANG_URL|g" $(rbenv root)/plugins/ruby-build/share/ruby-build/*; \
    fi; \
    rbenv install ${ATOM_RUBY_VERSION} -- --disable-install-doc \
 && if [ -n "$RUBY_LANG_URL" ]; then \
      sed -i "s|$RUBY_LANG_URL|https://cache.ruby-lang.org|g" $(rbenv root)/plugins/ruby-build/share/ruby-build/*; \
    fi; \
    ruby --version \
 && java --version \
 && rm -rf $HOME/.npmrc $HOME/.pip \
 && zypper clean -a \
 && if [ -n "$RUBYGEMS_REPO" ]; then \
      gem sources --add https://rubygems.org/; \
      gem sources --remove $RUBYGEMS_REPO/; \
      gem sources -c; \
    fi; \
    if [ -n "$SUSE_REPO" ]; then \
      sed -i "s|$SUSE_REPO|https://public-dl.suse.com|g" /etc/zypp/repos.d/*; \
    fi

CMD ["/bin/bash"]



# cdxgen-image
FROM base AS cdxgen

ENV CDXGEN_GEM_HOME="/tmp/gems" \
    CDXGEN_IN_CONTAINER=true \
    NODE_COMPILE_CACHE="/opt/cdxgen-node-cache" \
    PATH=${PATH}:/opt/cdxgen/node_modules/.bin

COPY . /opt/cdxgen

RUN set -e; \
    if [ -n "$NODEJS_DIST_URL" ]; then \
      export "NVM_NODEJS_ORG_MIRROR=$NODEJS_DIST_URL"; \
      printf "disturl=$NODEJS_DIST_URL\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$NPM_REPO" ]; then \
      export "COREPACK_NPM_REGISTRY=$NPM_REPO"; \
      printf "registry=$NPM_REPO\n@jsr:registry=$NPM_REPO\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$PIP_CONFIG" ]; then \
      mkdir $HOME/.pip/; \
      printf "$PIP_CONFIG" > $HOME/.pip/pip.conf; \
    fi; \
    if [ -n "$SUSE_REPO" ]; then \
      sed -i "s|https://public-dl.suse.com|$SUSE_REPO|g" /etc/zypp/repos.d/*; \
      zypper --no-gpg-checks refresh; \
    fi; \
    cd /opt/cdxgen \
 && corepack enable \
 && corepack pnpm install:prod \
 && corepack pnpm cache delete \
 && npm install -g --omit=dev @appthreat/atom-parsetools@1.0.4 \
 && mkdir -p ${NODE_COMPILE_CACHE} \
 && node /opt/cdxgen/bin/cdxgen.js --help \
 && rbastgen --help \
 && rm -rf ${CDXGEN_GEM_HOME} \
 && mkdir -p ${CDXGEN_GEM_HOME} \
 && rm -rf $HOME/.npmrc $HOME/.pip /root/.cache/node \
 && chmod a-w -R /opt \
 && zypper clean -a \
 && if [ -n "$SUSE_REPO" ]; then \
      sed -i "s|$SUSE_REPO|https://public-dl.suse.com|g" /etc/zypp/repos.d/*; \
    fi

WORKDIR /app

ENTRYPOINT ["node", "/opt/cdxgen/bin/cdxgen.js"]
